package miracle.joker.design.memento;

import java.util.Stack;

public class TextEditor {
    private String text;

    private Memento memento;

    public Memento getMemento() {
        return memento;
    }

    public void setMemento(Memento memento) {
        this.memento = memento;
    }

    private Stack<Memento> history = new Stack<>();

    public TextEditor(String text) {
        this.text = text;
        save();
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
        save();
    }

    public void undo() {
        if (!history.isEmpty()) {
            Memento m = history.pop();
            text = m.getText();
        }
    }

    public void redo() {
        if (!history.isEmpty()) {
            Memento m = history.peek();
            text = m.getText();
        }
    }

    private void save() {
        Memento m = new Memento(text);
        history.push(m);
    }

    public Stack<Memento> getHistory() {
        return history;
    }

    public void setHistory(Stack<Memento> history) {
        this.history = history;
    }
}
